// composables/useI18n.js
import {
	getCurrentInstance,
	computed
} from 'vue'
import {
	useStore
} from 'vuex'
export default function useI18n() {
	const store = useStore()
	const currentLang = computed(() => store.state.i18n.lang)
	const {
		proxy
	} = getCurrentInstance() || {}
	const t = (key) => proxy?.$t?.(key) || key // 防止未定义
	const changeLanguage = (lang) => {
		store.dispatch('i18n/changeLanguage', lang).then(() => {
			updateTabBarTexts() // 语言切换后自动更新 tabBar
		})
	}

	//tabBar 多语言支持
	const updateTabBarTexts = () => {
		const tabConfig = [{
				index: 0,
				key: 'menu1'
			},
			{
				index: 1,
				key: 'menu2'
			},
			{
				index: 2,
				key: 'menu3'
			},
			{
				index: 3,
				key: 'menu4'
			},
			{
				index: 4,
				key: 'menu5'
			}
		]

		tabConfig.forEach(tab => {
			uni.setTabBarItem({
				index: tab.index,
				text: t(tab.key)
			})
		})
	}
	return {
		t,
		currentLang,
		changeLanguage,
		updateTabBarTexts
	}
}